package com.infodms.dms.actions.claim.application;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.bean.ClaimListBean;
import com.infodms.dms.bean.TtAsWrApplicationExtBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.getCompanyId.GetOemcompanyId;
import com.infodms.dms.dao.claim.application.ClaimManualAuditingDao;
import com.infodms.dms.dao.claim.dealerClaimMng.ClaimBillMaintainDAO;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.FsFileuploadPO;
import com.infodms.dms.po.TcCodePO;
import com.infodms.dms.po.TmBusinessChngCodePO;
import com.infodms.dms.po.TtAsWrAppauthitemPO;
import com.infodms.dms.po.TtAsWrApplicationExtPO;
import com.infodms.dms.po.TtAsWrAuthinfoPO;
import com.infodms.dms.po.TtAsWrNetitemExtPO;
import com.infodms.dms.po.TtAsWrOtherfeePO;
import com.infodms.dms.po.TtAsWrOutrepairPO;
import com.infodms.dms.po.TtAsWrWrauthorizationPO;
import com.infodms.dms.util.CommonUtils;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PageResult;
import com.infodms.dms.po.TmVehiclePO;
/**
 * 
* @ClassName: ClaimBillStatusTrack 
* @Description: TODO(索赔单状态跟踪) 
* @author wangchao 
* @date Jun 23, 2010 3:13:56 PM 
*
 */
public class ClaimBillStatusTrack {
	private Logger logger = Logger.getLogger(ClaimBillStatusTrack.class);
	private final ClaimBillMaintainDAO dao = ClaimBillMaintainDAO.getInstance();
	private final String CLAIM_BILL_URL = "/jsp/claim/application/claimBillStatusTrack.jsp";// 主页面（查询）
	private final String CLAIM_BILL_DETAIL_URL = "/jsp/claim/application/claimBillStatusDetail.jsp";// 明细页面
	/**
	 * 
	* @Title: claimBillForward 
	* @Description: TODO(索赔状态查询跳转) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void claimBillForward() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			//取得该用户拥有的产地权限
			String yieldly = CommonUtils.findYieldlyByPoseId(logonUser.getPoseId());
			act.setOutData("logonUser", logonUser.getName());
			act.setOutData("yieldly", yieldly);
			act.setForword(CLAIM_BILL_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔单状态跟踪");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	/**
	 * 
	* @Title: applicationQuery 
	* @Description: TODO(索赔申请单查询) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void applicationQuery() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		//String dealerId = logonUser.getDealerId();
		StringBuffer con = new StringBuffer();
		Date date = new Date();
		Long companyId=GetOemcompanyId.getOemCompanyId(logonUser);     //公司ID
		Map<String,String> map = new HashMap<String,String>();
		try {
			RequestWrapper request = act.getRequest();
			Integer curPage = request.getParamValue("curPage") != null ? Integer
					.parseInt(request.getParamValue("curPage"))
					: 1; // 处理当前页
			String dealerCode = request.getParamValue("DEALER_CODE");
			String dealerName = request.getParamValue("DEALER_NAME");
			String roNo = request.getParamValue("RO_NO");
			String lineNo = request.getParamValue("LINE_NO");
			String claimNo = request.getParamValue("CLAIM_NO"); 
			String claimType = request.getParamValue("CLAIM_TYPE");
			String vin = request.getParamValue("VIN");
			String roStartdate = request.getParamValue("RO_STARTDATE");
			String roEnddate = request.getParamValue("RO_ENDDATE");
			String approveDate = request.getParamValue("approve_date");//审核时间
			String approveDate2 = request.getParamValue("approve_date2");
			String status = request.getParamValue("STATUS");
			String person = request.getParamValue("PERSON");
			String yieldly = request.getParamValue("YIELDLY");//查询条件--产地
			String yieldlys = CommonUtils.findYieldlyByPoseId(logonUser.getPoseId());//该用户拥有的产地权限
			
			List params = new LinkedList();
			//map.put("dealerId", dealerId);
			//map.put("claimNo", claimNo);
			map.put("dealerCode", dealerCode);
			map.put("dealerName", dealerName);
			map.put("roNo", roNo);
			map.put("lineNo", lineNo);
			map.put("claimType", claimType);
			map.put("vin", vin);
			map.put("roStartdate", roStartdate);
			map.put("roEnddate", roEnddate);
			map.put("status", status);
			map.put("oemCompanyId", companyId.toString());
			map.put("track", "track"); //状态跟踪查询
			map.put("yieldly", yieldly);
			map.put("yieldlys", yieldlys);
			map.put("person", person);
			map.put("approveDate", approveDate);
			map.put("approveDate2", approveDate2);
			map.put("claimNo", claimNo);
			PageResult<TtAsWrApplicationExtBean> ps = dao.queryApplication1(
					logonUser, map,params, Constant.PAGE_SIZE, curPage);
			act.setOutData("ps", ps);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔单状态跟踪");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	/**
	 * 
	* @Title: claimBillDetailForward 
	* @Description: TODO(索赔状态跟踪明细页面跳转) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void claimBillDetailForward() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			//TtAsActivityPO activity = new TtAsActivityPO();
			
			ClaimManualAuditingDao auditingDao = new ClaimManualAuditingDao();
			String id = request.getParamValue("ID");
			String isAudit = request.getParamValue("isAudit");//是否是索赔审核（true:是，false:索赔明细查询）
			TtAsWrApplicationExtPO tawep = auditingDao.queryClaimOrderDetailById(id);
			List<Map<String, Object>> customerList = dao.getVinUserName2(id);
			Map<String,Object> customerMap = new HashMap<String, Object>();
			if(customerList!=null && customerList.size()>0)
				customerMap = customerList.get(0);
			
			/******add by liuxh 20101125 roduct date*****/
			TmVehiclePO veh=new TmVehiclePO();
			veh.setVin(tawep.getVin());
			TmVehiclePO vehSel=(TmVehiclePO)auditingDao.select(veh).get(0);
			/******add by liuxh 20101125 product date*****/
			
			//zhumingwei 2011-03-04
			String count = auditingDao.countRepairTimes(id);
			//zhumingwei 2011-03-04
			
		    //取的当前需要审核的级别
		    String authCode = tawep.getAuthCode();
	        String authLevel = "--";
	        if(authCode!=null){
	    	    TtAsWrAuthinfoPO conditionPO = new TtAsWrAuthinfoPO();
	    	    conditionPO.setApprovalLevelCode(authCode);
	    	    List<TtAsWrAuthinfoPO> authLevelList = auditingDao.select(conditionPO);
	    	    if(authLevelList!=null && authLevelList.size()>0){
	    		    TtAsWrAuthinfoPO authInfoPO = authLevelList.get(0);
	    		    authLevel = CommonUtils.checkNull(authInfoPO.getApprovalLevelName());
	    	    }
	        }
			//取得产地名称
			String yieldly = tawep.getYieldly();
			String yieldlyName = "";
			if(yieldly!=null){
				TcCodePO codePO = CommonUtils.findTcCodeDetailByCodeId(CommonUtils.parseInteger(yieldly));
				if(codePO!=null && codePO.getCodeDesc()!=null)
					yieldlyName = codePO.getCodeDesc();
			}
			tawep.setYieldlyName(yieldlyName);
			
			List<ClaimListBean> partls = dao.queryPartById(id); //取配件信息
			List<ClaimListBean> itemls = dao.queryItemById(id); //取工时
			List<TtAsWrNetitemExtPO> otherls = dao.queryOtherByid(id);//取其他项目
			List<FsFileuploadPO> attachLs = dao.queryAttById(id);//取得附件
			List<TtAsWrAppauthitemPO> appAuthls = dao.queryAppAuthInfo(id);//审核授权信息
			TtAsWrWrauthorizationPO authReason = dao.queryAuthReason(id);//需要授权原因
			//List feeType = dao.getFeeType(Constant.FEE_STATUS.toString());//保养类型列表 
			//act.setOutData("OTHERFEE", getOtherfeeStr());
			
			String roNo = tawep.getRoNo();
			//2010-08-18 XZM 屏蔽页面不用显示质损相关信息，先不用显示，同时屏蔽页面隐藏域信息
			//act.setOutData("BUSINESS_CHNG_CODE_01",getChngCodeStr(Constant.BUSINESS_CHNG_CODE_01)); // 质损程度
			//act.setOutData("BUSINESS_CHNG_CODE_02",getChngCodeStr(Constant.BUSINESS_CHNG_CODE_02));// 质损区域
			//act.setOutData("BUSINESS_CHNG_CODE_03",getChngCodeStr(Constant.BUSINESS_CHNG_CODE_03));// 质损类型
			//act.setOutData("BUSINESS_CHNG_CODE_04",getChngCodeStr(Constant.BUSINESS_CHNG_CODE_04));// 故障代码
			act.setOutData("application", tawep);
			
			//如果是外出维修，查询外出维修相关信息
		      if(Constant.CLA_TYPE_09.toString().equals(tawep.getClaimType().toString())){
			      List outList = dao.getOutInfo(id);
			      if(outList.size()>0){
			    	  act.setOutData("outInfo", (TtAsWrOutrepairPO)outList.get(0));
			      }
		      }
			/*//2010-08-17 XZM 对应信息不在索赔单明细中呈现
			if (tawep!=null) {
			  //如果类型为免费保养
			  if (Constant.CLA_TYPE_02.equals(tawep.getClaimType())) {
				String flagStr = " in ";//默认设置保养费用
				
				HashMap hm = null;
				StringBuffer sb = new StringBuffer();
				List  params = new LinkedList();
				Long groupId = tawep.getModelId();
				if (groupId!=null) {
					sb.append(" and tvmg.group_id = ? ");//拼sql的查询条件：车型id
					params.add(groupId);
				}
				PageResult<Map<String, Object>> rs = dao.feeQuery(Constant.PAGE_SIZE, 1,sb.toString(),params,Constant.FEE_STATUS.toString(),flagStr);
				List ls = rs.getRecords();
				if (ls!=null && ls.size()>0){//车型保养费用存在
						 hm = (HashMap)ls.get(0);
				}
				else{//不存在，构造
					hm = new HashMap();
					for(int i=0;i<feeType.size();i++){
						HashMap map = (HashMap)feeType.get(i);
						hm.put(map.get("CODE_ID"), "");
					}
				}
				act.setOutData("FEE", hm);//保养参数对应的值
				
			}
			if (Constant.CLA_TYPE_06.equals(tawep.getClaimType())) {
				List<TtAsActivityPO> ls = dao.queryActivity(tawep.getId());
				if (ls!=null) {
					if (ls.size()>0) {
						
						activity = ls.get(0);
						
					}
				}
				
			}
			}*/
			//act.setOutData("ACTIVITY",activity );
			act.setOutData("attachLs", attachLs);
			//act.setOutData("FEETYPE", feeType);//查询动态列的集合
			act.setOutData("ID", id);//索赔申请单ID
			act.setOutData("itemLs", itemls);//索赔单之工时信息
			act.setOutData("partLs", partls);//索赔单之配件信息
			act.setOutData("otherLs",otherls);//索赔单之其他项目信息
			act.setOutData("appAuthls",appAuthls);//索赔单之审核信息
			act.setOutData("authReason",authReason);//索赔单之审核原因
			act.setOutData("isAudit", isAudit);//true:审核页面 false:明细查询页面
			act.setOutData("customerMap", customerMap);//客户信息
			act.setOutData("authLevel", authLevel);//需要审核的级别
			
			//zhumingwei 2011-03-04
			act.setOutData("count", count);//维修次数
			//zhumingwei 2011-03-04
			
			/********add by liuxh 20101125 *******/
			act.setOutData("vehSel", vehSel);//add vehcel
			/********add by liuxh 20101125 *******/
			//zhumingwei 2011-03-11  此方法用于区分轿车和微车
		    TcCodePO codePo= new TcCodePO();
		    codePo.setType(Constant.chana+"");
		    TcCodePO poValue = (TcCodePO)dao.select(codePo).get(0);
			String codeId = poValue.getCodeId();
			act.setOutData("codeId", Integer.parseInt(codeId));
			//zhumingwei 2011-03-11
			act.setForword(CLAIM_BILL_DETAIL_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔单状态跟踪");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	/**
	 * 
	 * @Title: getOtherfeeStr
	 * @Description: TODO(取得其他费用下拉框)
	 * @param
	 * @param type
	 * @param
	 * @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public String getOtherfeeStr() {
		ActionContext ctx = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) ctx.getSession().get(
				Constant.LOGON_USER);
		Long companyId=GetOemcompanyId.getOemCompanyId(logonUser);     //公司ID
		List<TtAsWrOtherfeePO> seriesList = dao.queryOtherFee(companyId);
		String retStr = "";
		retStr += "<option value=\'\'>-请选择-</option>";
		for (int i = 0; i < seriesList.size(); i++) {
			TtAsWrOtherfeePO bean = new TtAsWrOtherfeePO();
			bean = (TtAsWrOtherfeePO) seriesList.get(i);
			retStr += "<option value=\'" + bean.getFeeId() + "\'" + "title=\'"
					+ bean.getFeeName() + "\'>" + bean.getFeeCode()
					+ "</option>";
		}
		return retStr;
	}
	/**
	 * 
	 * @Title: getChngCodeStr
	 * @Description: TODO(取得故障代码，质损区域，质损类型，质损程度下拉框)
	 * @param
	 * @param type
	 * @param
	 * @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public String getChngCodeStr(int type) {
		ActionContext ctx = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) ctx.getSession().get(
				Constant.LOGON_USER);
		Long companyId=GetOemcompanyId.getOemCompanyId(logonUser);     //公司ID
		List<TmBusinessChngCodePO> seriesList = dao.queryChngCodeByType(type,companyId,null,null);
		String retStr = "[{";
		//retStr += "<option value=\'\'>-请选择-</option>";
		for (int i = 0; i < seriesList.size(); i++) {
			TmBusinessChngCodePO bean = new TmBusinessChngCodePO();
			bean = (TmBusinessChngCodePO) seriesList.get(i);
			retStr +=  ",'"+bean.getCode() + "':\'"
					+ bean.getCodeName() + "\'";
		}
		retStr += "}]";
		retStr = retStr.replaceFirst(",", "");
		return retStr;
	}
}


